package com.jsmy.marketing.fg;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.jsmy.marketing.R;
import com.jsmy.marketing.activity.CustomerActivity;
import com.jsmy.marketing.adapter.ListenerAdapter;
import com.jsmy.marketing.api.RetrofitManager;
import com.jsmy.marketing.bean.ListenerBean;
import com.jsmy.marketing.common.Link;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;

/**
 * Created by PC_WLT on 2018/8/27.
 */

public class CustomerListenerFragment extends BaseFragment {

    @BindView(R.id.my_recycler)
    RecyclerView myRecycler;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    ListenerAdapter adapter;
    List<ListenerBean.DataBean.ListBean> list;
    int pageindex = 1;


    @Override
    protected int resLayout() {
        return R.layout.fg_customer_listener_layout;
    }

    @Override
    protected void initData() {
        adapter = new ListenerAdapter();
        myRecycler.setLayoutManager(new LinearLayoutManager(getContext()));
        myRecycler.setAdapter(adapter);
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                pageindex = 1;
                 update();
            }
        });
        refreshLayout.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                pageindex++;
                update();

            }
        });
        update();
    }
    @Override
    public void update() {
        Map<String, Object> map = new HashMap<>();
        map.put("lid", com.jsmy.marketing.manager.UserManager.getInstance().getId());
        map.put("xsid", ((CustomerActivity)getActivity()).kid);
        map.put("pageindex", "" + pageindex);
        mPresenter.loadData(RetrofitManager.getInstance().getxsstlist(map), Link.APP_getxsstlist);
    }

    @Override
    public void refreshData(Object response, String position) {
         switch (position){
             case Link.APP_getxsstlist:
                 ListenerBean listenerBean = (ListenerBean) response;
                 list = listenerBean.getData().getList();
                 if (pageindex == 1) {
                     refreshLayout.finishRefresh();
                     adapter.replaceData(list);
                 } else {
                     refreshLayout.finishLoadmore();
                     adapter.addData(list);
                 }
                 break;
         }
         if (refreshLayout.isRefreshing()){
             refreshLayout.finishRefresh();
         }
    }
}
